package com.bdqn.controller;

import com.bdqn.dao.PhoneKcMapper;
import com.bdqn.pojo.Brandpojo;
import com.bdqn.pojo.PagePojo;
import com.bdqn.pojo.PhoneKcPojo;
import com.bdqn.util.ExprotPOIUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller//配置类
public class PhonekcController {
    @Resource
    PhoneKcMapper dao;

    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(int index,int bid,String name,HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        Object [] obj=null;
        String sql ="";
        if(bid != 0){
            sql = "phonekc where bid = "+bid;
        }else if (name != ""){
            sql = "phonekc where name like '%"+name+"%'";
        }else {
            sql = "phonekc";
        }
        obj = dao.getList(index,sql);
        List<PhoneKcPojo> list = (List<PhoneKcPojo>) obj[0];
        int count = ((ArrayList<Integer>)obj[1]).get(0);
        PagePojo page = new PagePojo(index,count,count%10==0?count/10:count/10+1,list);
        return page;
    }

    @RequestMapping("/getBrand1")
    @ResponseBody
    public Object getBrand(HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        List<Brandpojo> brand = dao.getBrand();
        return brand;
    }

    @RequestMapping("/excel")
    @ResponseBody
    public void getList1(int bid,String name,HttpServletResponse response) throws IOException, InterruptedException {
        List<PhoneKcPojo> list =null;
        if(bid != 0){
            list = dao.getExcelBid(bid);
        }else if (name != ""){
            list = dao.getExcelName("%"+name+"%");
        }else {
            list = dao.getExcel();
        }
        String [] arr = {"id","brand","name","ram","gb","color","price","num"};
        String [] keys = {"id","brand","name","ram","gb","color","price","num"};
        Thread.sleep(5000L);
        ExprotPOIUtils.start_download(response,"测试文件",list,arr,keys);
    }
}
